package com.hbpa.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.hbpa.dao.AdminPageDao;
import com.hbpa.form.AdminLoginAcc; 
import com.hbpa.util.ConnectionFactory;
import com.hbpa.util.JdbcUtil;

public class AdminPageDaoImpl implements AdminPageDao {
	public final int admin_page = 10; 
	
	public List<AdminLoginAcc> AdminLogAcc() {
		int startItem;
		int endItem; 
		int totalSize = getTotalSize();
		List<AdminLoginAcc> accs = null;
		
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		AdminLoginAcc acc = null;
		try {
			accs = new ArrayList<AdminLoginAcc>();
			con = ConnectionFactory.getConnection(); 
			ps = con.prepareStatement("select * from admin_acc order by id");
			
			rs = ps.executeQuery();
			while(rs.next())
			{ 
				 acc = new AdminLoginAcc(); 
				 acc.setXm(rs.getString("xm"));
				 acc.setPass(rs.getString("pass"));
				 accs.add(acc);
			}
			 return accs;
	} catch (SQLException e) { 
		e.printStackTrace();
	}finally{
		JdbcUtil.close(rs,ps,con);
	} 
		return null;
	}

	public List<AdminLoginAcc> getAdminLogAccByPage(int page) {
		int startItem;
		int endItem; 
		int totalSize = getTotalSize();
		List<AdminLoginAcc> accs = null;
		
		if(page>getTotalPage())
		{
			return null;
		}else if(page == 1 && totalSize < admin_page*page){
			startItem = 0; 
			endItem = totalSize;
		}else if(totalSize < admin_page*page){
			startItem = admin_page*(page-1); 
			endItem = totalSize-admin_page*(page-1);
		}else{
			startItem = admin_page*(page-1); 
			endItem =10;
		}
		
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		AdminLoginAcc acc = null;
		try {
			accs = new ArrayList<AdminLoginAcc>();
			con = ConnectionFactory.getConnection(); 
			ps = con.prepareStatement("select * from admin_acc order by id asc limit ?,?");
			
			ps.setInt(1, startItem);
			ps.setInt(2, endItem);
			
			rs = ps.executeQuery();
			
			while(rs.next())
			{ 
				 acc = new AdminLoginAcc();
				 acc.setPass(rs.getString("pass"));
				 acc.setXm(rs.getString("xm"));
				 accs.add(acc);
			}
			 return accs;
	} catch (SQLException e) { 
		e.printStackTrace();
	}finally{
		JdbcUtil.close(rs,ps,con);
	} 
		return null;
	}

	public int getTotalPage() {
		Connection con = null;
		Statement st = null;
		ResultSet rs = null;
		try {
			con = ConnectionFactory.getConnection(); 
			st = con.createStatement();
			String sql = "select count(*) as c from admin_acc order by id desc";
			rs = st.executeQuery(sql);
			if(rs.next())
			{ 
				String size = rs.getString("c");
				if(!size.equals("0")){
					int count = Integer.parseInt(size);
					if(count%admin_page == 0)
					{
						return count/admin_page;
					}else{
						return count/admin_page+1;
					}
				}else{
					return 0;
				}
			}
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,st,con);
		}
		return 0;
	}

	public int getTotalSize() {
		Connection con = null;
		Statement st = null;
		ResultSet rs = null;
		try {
			con = ConnectionFactory.getConnection(); 
			st = con.createStatement();
			String sql = "select count(*) as c from admin_acc order by id desc";
			
			rs = st.executeQuery(sql);
			if(rs.next())
			{ 
				String count = rs.getString("c");
				if(!count.equals("0")){
					return Integer.parseInt(count);
				}else{
					return 0;
				}
			}
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,st,con);
		}
		return 0;
	}
	
	public boolean deleteAdminLogAcc(String xm) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection(); 
			ps = con.prepareStatement("delete from admin_acc where xm=?");
			
			ps.setString(1, xm);
			boolean isDelete = ps.execute();
			
			return isDelete;
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(ps,con);
		}
		return false;
	}
}
